<template>
  <div class="hello">
    <el-container>
      <el-header style="height: 180px;width: 540px;margin: 20px auto;">
        <div class="title-container">
          <h3 class="title">高等教育作业管理系统</h3>
          <h3 class="title">学生登录</h3>
        </div>
      </el-header>
      <el-main style="margin: 0 auto;width: 540px;">
        <!--  学生登录 -->
        <el-form :model="studentForm" status-icon :rules="rules" ref="ruleForm" class="demo-ruleForm">
          <el-form-item label="账号" prop="studentNum">
            <el-input type="text" v-model="studentForm.studentNum"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="studentPassword">
            <el-input type="password" v-model="studentForm.studentPassword"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">立即登录</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
            <router-link to="/studentRegister">
              <el-button>前往注册</el-button>
            </router-link>
          </el-form-item>
        </el-form>
      </el-main>
      <el-footer style="width: 540px;margin: 10px auto;">
        <div>
          <router-link to="/adminLogin">
            <el-button type="primary" size="mini">管理员登录</el-button>
          </router-link>
          <router-link to="/leaderLogin">
            <el-button type="primary" size="mini">领导登录</el-button>
          </router-link>
          <router-link to="/teacherLogin">
            <el-button type="primary" size="mini">教师登录</el-button>
          </router-link>
          <router-link to="/teacherSecretaryLogin">
            <el-button type="primary" size="mini">教学秘书登录</el-button>
          </router-link>
          <router-link to="/studentLogin">
            <el-button type="primary" size="mini">学生登录</el-button>
          </router-link>
        </div>
      </el-footer>
    </el-container>
  </div>
</template>

<script>
export default {
  name: 'StudentLogin',
  data () {
    return {
      studentForm: {
        studentNum: '',
        studentPassword: ''
      },
      rules: {
        studentNum: [
          {required: true, message: '请输入账号', trigger: 'blur'},
          {min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur'}
        ],
        studentPassword: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur'}
        ]
      },
      loading: false,
      passwordType: 'studentPassword'
    }
  },
  created () {
    this.getStudentInfo()
  },
  methods: {
    getStudentInfo () {
      if (this.$route.query.studentInfo != null) {
        this.studentForm = this.$route.query.studentInfo
      } else {
        this.studentForm = {}
      }
    },
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post('http://localhost:8888/studentLogin', this.studentForm).then(res => {
            if (res.data.flag) {
              this.loading = true
              // 将学生的登录信息存到本地
              let student = JSON.stringify(res.data.data)
              localStorage.setItem('student', student)
              // 将登录信息存到vuex
              this.$store.commit('setStudent', res.data.data)
              // 登录成功提示信息
              this.$message({
                message: '恭喜你，登录成功',
                type: 'success'
              })
              // 登录成功前往教师端主页
              this.$router.push('/studentHome')
              // 清空表单信息
              this.studentForm.studentNum = ''
              this.studentForm.studentPassword = ''
            } else {
              // 清空表单信息
              this.$message.error('错了哦，登录失败')
              this.teacherForm.teacherNum = ''
              this.teacherForm.teacherPassword = ''
            }
          }).catch(err => {
            return Promise.reject(err)
          })
        } else {
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
  .el-header, .el-footer {
    background-color: #c6d1dc;
    color: #333;
    text-align: center;
    line-height: 60px;
  }

  .el-aside {
    background-color: #D3DCE6;
    color: #333;
    text-align: center;
    line-height: 200px;
  }

  .el-main {
    background-color: #E9EEF3;
    color: #333;
    text-align: center;
    line-height: 160px;
  }

  body > .el-container {
    margin-bottom: 40px;
  }

  .el-container:nth-child(5) .el-aside,
  .el-container:nth-child(6) .el-aside {
    line-height: 260px;
  }

  .el-container:nth-child(7) .el-aside {
    line-height: 320px;
  }

  .login-form {
    width: 500px;
  }
</style>
